﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Agency_TraCompany_ps_add : System.Web.UI.Page, HZ.MBSM.BLL.TraDes
{
    // int CWProduct;

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            //CWProduct = SysFun.ToInt(Request.QueryString["projectid"]);  //2007 11/30 马 项目合并
            daoru(); //导入
            databand();

        }


    }
    private void databand()
    {

        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyCompany";    //要查询的表明
        dbPage.Key = "AccountId_PS";        //表的主键 not null
        dbPage.Where = madeSql();        //查询条件  
        dbPage.SelectField = "ID,AccountId_PS, case  when  SupplyStatus='1' then '可配送' when  SupplyStatus='2'then '不可配送' end SupplyStatus,CompanyName_PS,UserCode_PS ";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);  //要显示第几页 
        dbPage.PageSize = 15;        //没有显示记录条数 
        dbPage.OrderBy = "CompanyName_PS,ID";   //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        company_list.DataSource = dr;
        company_list.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键


    }

    private string madeSql() //生成查询语句
    {
        string strSql = "", _company_name, _UserCode_PS;

        _company_name = Text_company_name.Text.ToString().Trim();
        _UserCode_PS = Text_usercode.Text.ToString().Trim();
        //strSql = strSql;// +"ProjectId =" + CWProduct;
        if (_company_name != "")
        {
            strSql = "and  CompanyName_PS like '%" + _company_name + "%' ";
        }

        if (_UserCode_PS != "")
        {
            strSql = strSql + "and UserCode_PS = '" + _UserCode_PS + "' ";
        }
        if (strSql != "")
        {
            strSql = strSql.Remove(0, 3);
        }

        return strSql;
    }
    #region TraDes 成员

    public string Name
    {
        get { return "配送企业管理"; }
    }

    public string Describe
    {
        get { return "配送企业管理"; }
    }

    public string ParentName
    {
        get { return "配送企业管理"; }
    }


    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            return _AccountType;
        }
    }
    #endregion
    protected void daoru()   //导入配送企业表
    {
        if (Request.Form["btnSubmit1"] == "导 入")
        {
            //  string strSql = "select count(Accountid_ps) as id from SupplyCompany where ProjectId="+CWProduct;
            string strSql = "select count(Accountid_ps) as id from SupplyCompany ";
            int nNumber = 0;
            Db db = new Db();
            System.Data.IDataReader dr;
            dr = db.DataReader(strSql);
            while (dr.Read())
            {
                nNumber = Convert.ToInt32(dr["id"].ToString());
            }
            dr.Dispose();
            if (nNumber > 0)
            {
                Response.Write("<script>window.alert('已导入过了！');</script>");
            }
            else
            {
                strSql = " insert into SupplyCompany(projectid,accountid_ps,companyname_ps,usercode_ps) select   8,AccountId, CompanyName,UserCode FROM dbo.Company where usercode is not null and CheckStatus=1 and Sort = 1";
                db.Command(strSql);
                strSql = "select count(Accountid_ps) id from dbo.SupplyCompany ";
                dr = db.DataReader(strSql);
                while (dr.Read())
                {
                    nNumber = Convert.ToInt32(dr["id"].ToString());
                }
                dr.Dispose();
                if (nNumber > 0)
                {
                    Response.Write("<script>alert('导入成功！');</script>");
                }
                else
                {
                    Response.Write("<script>alert('导入失败！');</script>");
                }
            }

            db.Close();

        }
        if (Request.Form["btnSubmit2"] == "取消资格")
        {
            string strAccountId_PS, strSql;
            if (Request.Form["chk"] == null)
            {
                Response.Write("<script>alert('请选择要撤配的企业！');</script>");
            }
            else
            {
                strAccountId_PS = Request.Form["chk"].ToString();
                Db db = new Db();
                string[] dd = strAccountId_PS.Split(',');
                strAccountId_PS = "";
                for (int i = 0; i < dd.Length; i++)
                {
                    strAccountId_PS = strAccountId_PS + "','" + dd[i];
                }
                strSql = "update SupplyCompany set SupplyStatus=2 where  AccountId_PS in ('" + strAccountId_PS + "')";
                db.Command(strSql);
                db.Close();
            }
        }
        if (Request.Form["btnSubmit3"] == "恢复资格")
        {
            string strAccountId_PS, strSql;
            if (Request.Form["chk"] == null)
            {
                Response.Write("<script>alert('请选择要恢复的企业！');</script>");
            }
            else
            {
                strAccountId_PS = Request.Form["chk"].ToString();
                Db db = new Db();
                string[] dd = strAccountId_PS.Split(',');
                strAccountId_PS = "";
                for (int i = 0; i < dd.Length; i++)
                {
                    strAccountId_PS = strAccountId_PS + "','" + dd[i];
                }
                strSql = "update SupplyCompany set SupplyStatus=1 where  AccountId_PS in ('" + strAccountId_PS + "')";
                db.Command(strSql);
                db.Close();
            }
        }

    }
    protected void add_Click(object sender, EventArgs e)  //添加配送企业
    {
        string strSql = "select count(AccountId) id from Company where AccountId not in (select AccountId_PS from dbo.SupplyCompany  ) and usercode is not null and CheckStatus=1 and Sort =1 ";
        int nNumber = 0;
        Db db = new Db();
        System.Data.IDataReader dr;
        dr = db.DataReader(strSql);
        while (dr.Read())
        {
            nNumber = Convert.ToInt32(dr["id"].ToString());
        }
        dr.Dispose();
        dr.Close();
        db.Close();
        if (nNumber == 0)
        {
            Response.Write("<script>window.alert('没有新的配送企业！');</script>");
        }
        else
        {
            Response.Write("<script>window.open('Tra_add_supplyCompany.aspx')</script>");
        }

    }
}
